package com.workerhelping.controller;

import com.workerhelping.elasticsearchEntity.ESIncomeVO;
import com.workerhelping.result.Result;
import com.workerhelping.service.ESIncomeVOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ESIncome")
public class ESIncomeVOController {

    @Autowired
    private ESIncomeVOService esIncomeVOService;

    @PostMapping("/insertIncome")
    public Result<String> insertIncome(@RequestBody ESIncomeVO esIncomeVO) {
        try {
            esIncomeVOService.insert(esIncomeVO);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("/search")
    public Result<List<ESIncomeVO>> searchIncome(@RequestParam(value = "sId",required = false) Integer id,
                                                 @RequestParam(value = "startDate",required = false) String startDate,
                                                 @RequestParam(value = "endDate",required = false) String endDate,
                                                 @RequestParam(value = "sYear",required = false) String year,
                                                 @RequestParam(value = "minPrice",required = false) Double minPrice,
                                                 @RequestParam(value = "maxPrice",required = false) Double maxPrice,
                                                 @RequestParam(value = "sCategory",required = false) String category) {
        try {
            List<ESIncomeVO> incomes = esIncomeVOService.search(id, startDate, endDate, year, minPrice, maxPrice, category);
            return Result.ok(incomes);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
